<#include "../common/_layout.html" />
<@head title="权限分配">
<style type="text/css">
	.cll {
		background-color: #ddd;
	}
</style>
<link rel="stylesheet" href="${ctx}/adminlte/plugins/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
</@head>
<@layout menu='/role'>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<div class="box">
			角色名:<label for="adminname">${role.name!}</label>
				<div>
   <ul id="treeDemo" class="ztree"></ul>
</div>
			
			
	<input type="hidden" id="roleid" name="roleid" value=${role.id}>
	<button type="button" class="btn btn-info" onclick="saveroleresource('${ctx}/roleresource/save')" >保存</button><button type="submit" class="btn btn-cancle" onclick="javascript :history.back(-1);">返回</button>		
				
			
			</div>
		</div>
	</div>
</section>
</@layout>
<@scripts>
<script type="text/javascript" src="${ctx}/adminlte/plugins/bootbox/bootbox.min.js"></script>
<script type="text/javascript" src="${ctx}/js/list.js"></script>
 <script type="text/javascript" src="${ctx}/adminlte/plugins/zTree/js/jquery.ztree.core.js"></script>
  <script type="text/javascript" src="${ctx}/adminlte/plugins/zTree/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript">
var zTreeObj;
var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		}
	};

function saveroleresource(url) {
	var roleid = $("#roleid").val();
var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	var allresourceids=[];
	var checkedresourceids=[];
	var nodes = treeObj.transformToArray(treeObj.getNodes());
	for(var i=0;i<nodes.length;i++){
		allresourceids[i]=nodes[i].id;
      }
 var checkednodes = treeObj.getCheckedNodes(true);
 for(var i=0;i<checkednodes.length;i++){
	 checkedresourceids[i]=checkednodes[i].id; 
   }

	$.ajax({
		type : "post",
		url : url,
		data : {
			roleSet_id : roleid,
			ids : checkedresourceids,
			allids : allresourceids
		},
		dataType : "json",
		async : false,
		success : function(data) {
			bootbox.alert(data.message,function(){
				location.href="${ctx}/role";
				
			});
			
		}
	});
}
	$(document).ready(function(){
		var roleid=$("#roleid").val();
		$.get("${ctx}/role/resourcetree",{id:parseInt(roleid)},function(data){
			var znodes=$.parseJSON(data);
			 zTreeObj = $.fn.zTree.init($("#treeDemo"), setting,znodes);	
		});
		
		
		 
	});
</script>
</@scripts>